package com.two.screen.water.balance.net.api

import com.two.screen.water.balance.bean.IndexBean
import com.two.screen.water.balance.bean.PayButton
import com.two.screen.water.balance.bean.VideoBean
import com.two.screen.water.balance.bean.database.ProductBean
import com.two.screen.water.balance.bean.water.BuyerBean
import com.two.screen.water.balance.bean.water.SellerBean
import com.two.screen.water.balance.print.android_print.bluetooth.BluetoothPrinter
import com.two.screen.water.balance.utils.Params
import com.two.screen.water.balance.utils.SPUtils
import com.two.screen.water.balance.utils.UniqueIDUtils
import com.two.screen.water.balance.utils.eventbus.Event
import com.two.screen.water.balance.utils.eventbus.EventBusUtil
import com.two.screen.water.balance.utils.eventbus.EventCode
import java.util.*

object DataApi {
    /*设备ID*/
    var mAppUnitId: String = ""

    /*全局api地址*/
    var mApiUrl: String = ""

    /*全局图片地址*/
    var mImageUrl: String = ""

    /*是否脱机操作*/
    var mIsOffline: Boolean = false

    /*是否有网*/
    var mIsConnectNet: Boolean = false

    /*订单溯源前缀地址*/
    var mOrderTraceQrPrefixUrl: String = ""

    /*商户小程序二维码地址*/
    var mShopWetchQrUrl: String? = null//"http://weixin.qq.com/r/ax27o7PEz_6wrffA90h5"

    /*市场名称*/
    //var mMarketName: String = "食安慧·市场"
    var mMarketName: String = "暂无"

    /*市场id*/
    var mMarketId: String = ""

    /*操作员名称*/
    var mOperationUserName: String = ""

    /*操作员id*/
    var mOperationUserId: String = ""

    /*操作员名称*/
    var mUserName: String? = null

    /*操作员id*/
    var mUserId: String? = null

    /*是否允许修改重量 0 不允许 1 允许*/
    var mIsWeight = false

    /*预留字段 登陆是返回*/
    var mToken: String? = ""

    /*打印开关*/
    var mIsOpenPrint: Boolean = false

    /*打印次数*/
    var mPrintTimes: Int = 1

    /*打印小票 头部提示*/
    var mPrintHeadTips: String = "溯源小票"

    /*打印小票二维码 顶部提示*/
    var mPrintQrTopTips: String = "溯源二维码"

    /*打印小票二维码 底部提示*/
    var mPrintQrBottomTips: String = "祝您购物愉快"

    /*0重、0购物车订单 多久进入待机 单位秒*/
    var mLeftTime2Await: Long = 5L

    /*语音播报开关*/
    var mIsOpenVoiceBroadcast: Boolean = false

    /*商品分类列表*/
    var mCateList: MutableList<ProductBean>? = null

    /*商品列表*/
    var mProductList: MutableList<ProductBean>? = null

    /*单位列表*/
    var mUnitList: MutableList<ProductBean>? = null

    /*副屏 称重情况下 底部滚播提示文字*/
    var mViceWeightBottomTipsListData: MutableList<String>? = /*null*/mutableListOf("公告：平等、公正、法治、爱国、敬业、诚信、友善。", "疫情防控：科学防治，战胜疫情，不信谣、不传谣。")

    /*副屏 称重情况下 底部滚播轮询时长 单位秒*/
    var mViceWeightBottomTipsLoopTime = 5

    /*副屏 称重情况下 广告滚播轮询时长 单位秒*/
    var mViceWeightBannerLoopTime = 5

    /*副屏 称重情况下 广告滚播动画类型 0~16种 默认推荐16*/
    var mViceWeightBannerAnimationType = 16

    /*副屏 称重情况下 广告图数据*/
    var mViceWeightBannerListData_Temp: MutableList<String> = mutableListOf(
        "https://jz-advert.oss-cn-shenzhen.aliyuncs.com/video/20231110/20231110095708_r6kh.png",
        "https://jz-advert.oss-cn-shenzhen.aliyuncs.com/video/20231110/20231110095730_okz9.png"
    )
    var mViceWeightBannerListData = mViceWeightBannerListData_Temp

    /*副屏 待机情况下 线上商场二维码地址*/
    var mViceAwaitOnlineUrlQr: String? = null//"http://img.jzxnm.com/uploads/jpg/2023-04-10/16811134690.jpg"

    /*副屏 待机情况下 线上商场二维码提示文字*/
    var mViceAwaitOnlineTips: String? = "一户一码"

    /*副屏 待机情况下 公众号二维码地址*/
    var mViceAwaitOfficialUrlQr: String? = null//"http://weixin.qq.com/r/ax27o7PEz_6wrffA90h5"

    /*副屏 待机情况下 公众号二维码提示文字*/
    var mViceAwaitOfficialTips: String? = "食安慧（深圳）科技股份有限公司\n联系电话:400-808-0738\nhttp://www.sahkj.cn"

    /*副屏 待机情况下 广告滚播动画类型 0~16种 默认推荐16*/
    var mViceAwaitBannerAnimationType = 16

    /*副屏 待机情况下 广告图数据*/
    var mViceAwaitBannerListData_Temp: MutableList<String> = mutableListOf(
        "http://img.jzxnm.com/uploads/images//xryH79A-PnUVRHRymAqG0T4xbL7tHuuD.jpg",
        "http://img.jzxnm.com/uploads/png/2023-02-16/16765241043.png",
        "http://img.jzxnm.com/uploads/images//ERoXIQCAdAy3oXUEs37XIDG_N_spmPRp.jpg",
        "http://img.jzxnm.com/uploads/png/2022-10-10/16653988353.png"
    )
    var mViceAwaitBannerListData = mViceAwaitBannerListData_Temp

    var mVideoBean: VideoBean? = null// VideoBean("http://jzxnm-app.oss-cn-shenzhen.aliyuncs.com/mp4/SAH_ADV.mp4", "http://img.jzxnm.com//uploads/images/other/2022-04/JDOsZlMfji1NxM0-3BoAYPmk7dI5nybn.jpg")

    /*副屏 待机情况下 广告滚播轮询时长 单位秒*/
    var mViceAwaitBannerLoopTime = 5

    /*店铺Logo地址*/
    //var mShopLogoUrl: String? = "http://img.jzxnm.com/uploads/gif/2023-05-17/16842891022.gif"
    var mShopLogoUrl: String? = null//"https://jz-advert.oss-cn-shenzhen.aliyuncs.com/video/20240408/20240408163328_ev3i.png"

    /*店铺联系人*/
    //var mShopUserName: String? = "大家好，我是渣渣昆"
    var mShopUserName: String? = "暂无"

    /*店铺名称*/
    //var mShopName: String? = "渣渣昆丶烧鸭店"
    var mShopName: String? = "暂无"

    /*店铺摊位号*/
    //var mShopStallName: String? = "7楼-中间A8"//"暂无"
    var mShopStallName: String? = "暂无"//"暂无"

    /*商户联系电话*/
    //var mShopStallName: String? = "7楼-中间A8"//"暂无"
    var mShopContactTel: String? = ""//"暂无"

    /*店铺评星*/
    var mShopStart: Float = 5f

    /*公司信息*/
    var mCompanyTips: String? = "食安慧（深圳）科技股份有限公司技术支持   联系电话：400-808-0738"

    /*静态 微信 支付二维码地址*/
    var mStaticPayWetchUrl: String? = ""//"暂无"

    /*静态 微信 支付二维码背景地址*/
    var mStaticPayWetchBgUrl: String? = ""

    /*静态 支付宝 支付二维码地址*/
    var mStaticPayAliUrl: String? = ""//"暂无"

    /*静态 支付宝 支付二维码背景地址*/
    var mStaticPayAliBgUrl: String? = ""

    /*静态 聚合 支付二维码地址*/
    var mStaticPayJuHeUrl: String? = null

    /*静态 聚合 支付二维码背景地址*/
    var mStaticPayJuHeBgUrl: String? = "http://img.jzxnm.com/uploads/png/2023-09-08/16941668743.png"

    /*动态 微会员 支付二维码背景地址*/
    var mDynamicPayVipBgUrl: String? = "http://img.jzxnm.com/uploads/png/2023-09-08/16941668742.png"

    /*动态 聚合 支付二维码背景地址*/
    var mDynamicPayJuHeBgUrl: String? = "http://img.jzxnm.com/uploads/png/2023-09-08/16941668749.png"


    /*本地循环上传时间段 每隔多久检查上传 单位秒*/
    var mLocalPollingUploadTime = 5L

    /*本地循环上传数据量 每次上传多少条 单位条*/
    var mLocalPollingUploadSize = 10

    /*本地循环上传数据量的上传失败的次数 单位次*/
    var mLocalPollingUploadFailedTimes = 2 //测试时候，调低点。

    var mPlayTimeBanner: Int = 60 //图片播放时长
    var mPlayTimeVideo: Int = 1800 //图片播放时长

    /*以下是2022年12月15日 09:44:37 新增 */
    /*打印数据*/
    var mPrinterTypeIndex = SPUtils.getInt(Params.mPrinterTypeIndex)
    var mPrinterTypeArray: Array<String> = arrayOf("不使用", "AutoReplyPrinter", "CommonPrinter", "AndroidPrinter")

    /*读卡器数据*/
    var mReaderIndex = SPUtils.getInt(Params.mReaderIndex) //1:深圳市昱闵科技有限公司-14443A系列
    var mReaderArray: Array<String> = arrayOf("不使用", "14443A系列")
    var mReaderSerialPortPath = SPUtils.getString(Params.mReaderSerialPortPath)//这个是选择串口地址，不选择就默认。

    /*电子秤数据*/
    var mScaleTypeIndex = SPUtils.getInt(Params.mScaleTypeIndex)//超赢、七华、顶尖
    var mScaleTypeArray: Array<String> = arrayOf("不使用", "SW", "QH", "DJ")
    var mWeightSerialPortPath = SPUtils.getString(Params.mWeightSerialPortPath)//这个是选择串口地址，不选择就默认。

    /*小票机打印张数*/
    var mBillPrintTimesIndex = SPUtils.getInt(Params.mBillPrintTimesIndex)
    var mBillPrintTimesArray: Array<String> = arrayOf("1", "2", "3")

    /*打印纸宽度*/
    var mPrintPaperWidthIndex = SPUtils.getInt(Params.mPrintPaperWidthIndex)
    var mPrintPaperWidthArray: Array<String> = arrayOf("58mm", "80mm")

    /*是否开启副屏显示*/
    var mIsShowViceDisplay = SPUtils.getBoolean(Params.mIsShowViceDisplay)

    /*是否开启电量显示*/
    var mIsShowBatteryChanged = SPUtils.getBoolean(Params.mIsShowBatteryChanged)

    /*是否开支付语音播报*/
    var mIsOpenPayVoice = true

    /*是否开启蓝牙打印显示*/
    var mIsShowBluetoothPrinter = SPUtils.getBoolean(Params.mIsShowBluetoothPrinter)

    /*是否开启副屏显示*/
    var mIsShowViceBanner = SPUtils.getBoolean(Params.mIsShowViceBanner)

    /*USB打印小票连接的路径*/
    var mUSBDevicePath = SPUtils.getString(Params.mUSBDevicePath)

    /*秤体串口连接的路径*/
    var mSerialPortPathWeight = SPUtils.getString(Params.mSerialPortPath)

    /*秤体串口连接的端口*/
    var mSerialPortPortWeight = SPUtils.getInt(Params.mSerialPortPort)//这个 是连接过后识存储起来的。

    /*秤体串口连接的路径*/
    var mSerialPortPathReader = SPUtils.getString(Params.mSerialPortPathReader)

    /*秤体串口连接的端口*/
    var mSerialPortPortReader = SPUtils.getInt(Params.mSerialPortPortReader)

    /*AI识别的厂家品牌*/
    var mAiBrandTypeIndex = SPUtils.getInt(Params.mAiBrandTypeIndex)
    var mAiBrandTypeArray: Array<String> = arrayOf("不使用", "SF", "YY", "YG", "CZ")

    /*广告播放模式*/
    var mBannerPlayTypeIndex = SPUtils.getInt(Params.mBannerPlayTypeIndex, 1)
    var mBannerPlayTypeArray: Array<String> = arrayOf("图片和视频循环轮播", "仅播放图片", "仅播放视频")

    /*副屏多久进入广告的时间*/
    var mIntoBannerTimeIndex = SPUtils.getInt(Params.mIntoBannerTimeIndex, 1)
    var mIntoBannerTimeArray: Array<String> = arrayOf("0", "15", "30", "60", "120", "300", "600")

    /*每张广告图轮播的时长(单位秒)*/
    var mPerBannerImgStayTimeIndex = SPUtils.getInt(Params.mPerBannerImgStayTimeIndex, 1)
    var mPerBannerImgStayTimeArray: Array<String> = arrayOf("0", "15", "30", "60", "120", "300", "600")

    /*收银支付按钮*/
    var mPayButtonList: MutableList<PayButton> = mutableListOf(PayButton().apply { pay_id = 1;title = "现金支付" })

    /*筛选 上传状态*/
    var mStateUploadArray: Array<String> = arrayOf("全部", "未上传", "已上传")

    /*筛选 支付状态*/
    var mStatePayArray: Array<String> = arrayOf("全部", "未支付", "已支付")

//    /*Ai 序列号配置*/
//    var mAiData: IndexBean? = null


    /*简易进销存 1 使用 2 不使用*/
    var mUseSimpleInventory = false

    /*====================华丽分割线 水产2024年11月14日 开始======================*/
    var mSellerInfo: SellerBean? = null
    var mBuyerInfo: BuyerBean? = null
    var mMerchantServiceFee: String? = null
    var mBuyServiceFee: String? = null

    fun resetData() {
        mSellerInfo = null
        mBuyerInfo = null
        mMerchantServiceFee = null
        mBuyServiceFee = null
    }


    fun initData() {
        /*初始化一些全局数据*/
        mIsOpenPayVoice = SPUtils.getBoolean(Params.mIsOpenPayVoice, true)
        mWeightSerialPortPath = SPUtils.getString(Params.mWeightSerialPortPath)
        mReaderSerialPortPath = SPUtils.getString(Params.mReaderSerialPortPath)
        mAppUnitId = UniqueIDUtils.getUniqueID().uppercase(Locale.getDefault())
        mIsOpenPrint = SPUtils.getBoolean(Params.mIsOpenPrintSw)
        mIsOpenVoiceBroadcast = SPUtils.getBoolean(Params.mIsOpenVoiceBroadcastSw)
        mPrintTimes = SPUtils.getInt(Params.mPrintTimesEt)
        BluetoothPrinter.mBluetoothPrinterName = SPUtils.getString(Params.mBluetoothPrinterName)
        BluetoothPrinter.mBluetoothPrinterMac = SPUtils.getString(Params.mBluetoothPrinterMac)

        mApiUrl = SPUtils.getString(Params.mApiUrl) ?: ""
        mImageUrl = SPUtils.getString(Params.mImageUrl) ?: ""

    }

    fun updateData(data: IndexBean) {
        mOrderTraceQrPrefixUrl = data.mOrderTraceQrPrefixUrl ?: mOrderTraceQrPrefixUrl
        mShopWetchQrUrl = data.basis?.com_url //一户一码
//        mOperationUserName = data.mOperationUserName ?: mOperationUserName
//        mOperationUserId = data.mOperationUserId ?: mOperationUserId
        //mToken = SPUtils.getToken() ?: mToken
        mPrintHeadTips = data.mPrintHeadTips ?: mPrintHeadTips
        mPrintQrTopTips = data.mPrintQrTopTips ?: mPrintQrTopTips
        mPrintQrBottomTips = data.cd_t ?: mPrintQrBottomTips
        mLeftTime2Await = data.mLeftTime2Await
        mIsOpenVoiceBroadcast = data.mIsOpenVoiceBroadcast == 1 //语音播报开关:0关闭，1打开
        //TODO:设置服务器数据
        //IndexApi.formatProductList(data)

        mViceWeightBottomTipsListData = data.mViceWeightBottomTipsListData
        mViceWeightBottomTipsLoopTime = data.mViceWeightBottomTipsLoopTime
        mViceWeightBannerLoopTime = data.mViceWeightBannerLoopTime
        mViceWeightBannerAnimationType = data.mViceWeightBannerAnimationType
        //mViceWeightBannerListData = data.mViceWeightBannerListData ?: mViceWeightBannerListData //非待机状态 右底广告图 原先
        val bottomAdvertList = data.basis?.bottom_advert_list
        mViceWeightBannerListData = if (bottomAdvertList.isNullOrEmpty()) mViceWeightBannerListData else data.basis?.bottom_advert_list!!   //非待机状态 右底广告图 2024年4月8日 16:24:44 接入接口


        //mViceAwaitOnlineUrlQr = data.mViceAwaitOnlineUrlQr ?: mViceAwaitOnlineUrlQr
        mViceAwaitOnlineUrlQr = data.basis?.com_url //一户一码
        mViceAwaitOnlineTips = data.basis?.com_url_title ?: mViceAwaitOnlineTips
        mViceAwaitOfficialUrlQr = data.basis?.com_url //一户一码
        mViceAwaitOfficialTips = data.mViceAwaitOfficialTips ?: mViceAwaitOfficialTips
        mViceAwaitBannerAnimationType = data.mViceAwaitBannerAnimationType
        //这里还需要追加营业执照
        var tempAdsBannerList = mutableListOf<String>()
        val certificate = data.basis?.certificate
        val license = data.basis?.license
        if (!certificate.isNullOrBlank()) {
            tempAdsBannerList.add(certificate)
        }
        if (!license.isNullOrBlank()) {
            tempAdsBannerList.add(license)
        }
        val scsAdvertList = data.basis?.scs_advert_list
        if (!scsAdvertList.isNullOrEmpty()) {
            tempAdsBannerList.addAll(scsAdvertList)
        }
        if (tempAdsBannerList.isNotEmpty()) {
            mViceAwaitBannerListData = tempAdsBannerList  //待机状态 广告图 2024-04-08 16:19:02 接入接口
        }
        //mViceAwaitBannerListData = data.mViceAwaitBannerListData  //待机状态 广告图 原先
        mVideoBean = data.mVideoBean ?: mVideoBean
        mViceAwaitBannerLoopTime = data.mViceAwaitBannerLoopTime
        //mShopLogoUrl = data.mShopLogoUrl ?: mShopLogoUrl
        mShopUserName = data.stall?.cm_cnm ?: mShopUserName
        mShopName = data.stall?.st_nm ?: mShopName
        mShopStallName = data.stall?.st_cd ?: mShopStallName
        mShopStart = data.stall?.st_lv ?: 5.0f

        mShopContactTel = data.stall?.cm_ph

        mMarketName = data.market?.mar_nm ?: mMarketName
        mMarketId = data.market?.mar_id ?: mMarketId
        mCompanyTips = data.mCompanyTips ?: mCompanyTips

        //TODO:这里应该还需要返回支付背景url,同时根据支付类型多少个来调整。
        mStaticPayWetchUrl = data.pay?.list_url?.get(0)?.url ?: mStaticPayWetchUrl
        mStaticPayWetchBgUrl = data.mStaticPayWetchBgUrl ?: mStaticPayWetchBgUrl

        mStaticPayAliUrl = data.pay?.list_url?.get(1)?.url ?: mStaticPayAliUrl
        mStaticPayAliBgUrl = data.mStaticPayAliBgUrl ?: mStaticPayAliBgUrl

        mStaticPayJuHeUrl = data.pay?.list_url?.get(2)?.url ?: mStaticPayJuHeUrl
        mStaticPayJuHeBgUrl = data.mStaticPayJuHeBgUrl ?: mStaticPayJuHeBgUrl

        mDynamicPayVipBgUrl = data.mDynamicPayVipBgUrl ?: mDynamicPayVipBgUrl
        mDynamicPayJuHeBgUrl = data.mDynamicPayJuHeBgUrl ?: mDynamicPayJuHeBgUrl
        mLocalPollingUploadTime = data.mLocalPollingUploadTime
        mLocalPollingUploadSize = data.mLocalPollingUploadSize
        mLocalPollingUploadFailedTimes = data.mLocalPollingUploadFailedTimes//测试时候，调低点。

        //这里处理Ai信息
        when (data.ai?.type_id) {
            1 -> {//食方科技
                SPUtils.putString(Params.mStoreNo, data.ai?.config?.mStoreNo)
                SPUtils.putString(Params.mLicenseNo, data.ai?.config?.mLicenseNo)
                SPUtils.putString(Params.mStoreName, data.ai?.config?.mStoreName)
            }
            2 -> {//由由

            }
            3 -> {//银歌

            }
            4 -> {//创智链科技

            }
            else -> {
                SPUtils.putString(Params.mStoreNo, null)
                SPUtils.putString(Params.mLicenseNo, null)
                SPUtils.putString(Params.mStoreName, null)
            }
        }
        mUseSimpleInventory = data.market?.is_sim_pasi == 1//简易进销存 1 使用 2 不使用

        //TODO:发消息通知刷新界面
        EventBusUtil.sendEvent(Event(EventCode.SYNC_NEW_DATA))
    }

    fun logoutViceScreenDisplayData() {
        mShopLogoUrl = null
        mUserName = null
        mShopUserName = "暂无"
        mShopName = "暂无"
        mShopStallName = "暂无"
        mShopStart = 5.0f

        /*静态码*/
        mStaticPayWetchUrl = null
        mStaticPayAliUrl = null
        mStaticPayJuHeUrl = null
        mDynamicPayVipBgUrl = null
        mViceWeightBannerListData = mViceWeightBannerListData_Temp
        mViceAwaitBannerListData = mViceAwaitBannerListData_Temp
        //TODO:发消息通知刷新界面
        EventBusUtil.sendEvent(Event(EventCode.SYNC_NEW_DATA))
    }
}